<template>
  <div class="app-container">
    <el-table :data="tableData" style="width: 100%" @cell-mouse-enter="setCarouselId">
      <el-table-column label="轮播图id" width="180" prop="carouselId"></el-table-column>
      <el-table-column label="轮播图名称" width="180" prop="carouselName"></el-table-column>
      <el-table-column label="图片" width="300">
        <template slot-scope="scope">
          <img :src="scope.row.carouselImgUrl" width="300" height="200" />
        </template>
      </el-table-column>
      <el-table-column label="重定向地址" width="180" prop="carouselRedirectUrl"></el-table-column>
      <el-table-column label="是否启用" width="180">
        <template slot-scope="scope">
          <el-switch
            ref="status"
            :value="getSatus(scope.row.carouselId)"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
            @change="handleChange"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑轮播图</el-button>
          <el-button size="mini" @click="handleDelete(scope.row.carouselId)">删除轮播图</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageParam.currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageParam.pageSize"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>

    <el-dialog title="编辑轮播图" :visible.sync="editVisible" width="30%">
      <el-form
        :model="editFrom"
        :rules="rules"
        ref="editFrom"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="轮播图名称" prop="carouselName">
          <el-input v-model="editFrom.carouselName"></el-input>
        </el-form-item>
        <el-form-item label="轮播图重定向地址" prop="carouselRedirectUrl">
          <el-input v-model="editFrom.carouselRedirectUrl"></el-input>
        </el-form-item>
        <el-form-item label="轮播图排序值" prop="carouselRank">
          <el-input v-model="editFrom.carouselRank"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('editFrom')">修改轮播图</el-button>
          <el-button @click="resetForm('editFrom')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPageCarsouel,
  changeStatus,
  update,
  delCarousel,
  getCount,
} from "@/api/carousel";
export default {
  data() {
    return {
      tableData: [],

      total: 0,

      // 编辑
      editVisible: false,

      rules: {
        carouselName: [
          { required: true, message: "名称不能为空", trigger: "blur" },
        ],
        carouselRedirectUrl: [
          { required: true, message: "地址不能为空", trigger: "blur" },
        ],
        carouselRank: [
          { required: true, message: "排序值不能空", trigger: "blur" },
        ],
      },

      editFrom: {
        carouselId: "",
        carouselName: "",
        carouselRedirectUrl: "",
        carouselRank: "",
      },

      pageParam: {
        currentPage: 1,
        pageSize: 5,
      },
    };
  },

  methods: {
    init() {
      console.log("初始化了");
      getPageCarsouel(this.pageParam).then((res) => {
        this.tableData = res.data;
      });
      getCount().then((res) => {
        this.total = res.data;
      });
    },

    handleSizeChange(val) {
      this.pageParam.pageSize = val;
      getPageCarsouel(this.pageParam).then((res) => {
        this.tableData = res.data;
      });
    },

    handleCurrentChange(val) {
      this.pageParam.currentPage = val;
      getPageCarsouel(this.pageParam).then((res) => {
        this.tableData = res.data;
      });
    },

    getSatus(id) {
      for (var item of this.tableData) {
        if (item.carouselId == id) {
          console.log(item.status);

          return item.status;
        }
      }
    },

    handleChange(value) {
      changeStatus(this.index, value).then((res) => {
        if (res.code == 200) {
          //遍历进行修改
          for (var item of this.tableData) {
            if (item.carouselId == this.index) {
              item.status = value;
              break;
            }
          }
        }
      });
    },

    handleEdit(index, row) {
      this.editVisible = true;
      this.editFrom = row;
    },

    handleDelete(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delCarousel(id).then((res) => {
            if (res.code == 200) {
              this.init();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          update(this.editFrom).then((res) => {
            if (res.code == 200) {
              this.init();
              this.editVisible = false;
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

    // 设置当前的轮播图id
    setCarouselId: function (row, column, cell, event) {
      this.index = row.carouselId;
    },
  },

  created() {
    this.init();
  },
};
</script>

<style>
</style>